package com.robusta.cityuate.dialogs;
import java.util.Calendar;

import com.robusta.cityuate.utils.TimeUtils;


import android.app.Dialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.format.DateFormat;
import android.widget.EditText;
import android.widget.TimePicker;

public class TimePickerFragment extends DialogFragment implements OnTimeSetListener
                            {

	public static final String TIME_PICKER_TAG = "timePicker";

	private static final String TIMEVIEW_ID = "timeview_ID";
    
	EditText timeView;
	int timeViewId;
	

	public static TimePickerFragment getInstance(int timeViewId){
		TimePickerFragment f=new TimePickerFragment();
		Bundle b=new Bundle();
		b.putInt(TIMEVIEW_ID, timeViewId);
		f.setArguments(b);
		return f;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		timeViewId=getArguments().getInt(TIMEVIEW_ID);
	}
	
	@Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
		
        // Use the current time as the default values for the picker
        Calendar c = Calendar.getInstance();
        timeView=(EditText) getActivity().findViewById(timeViewId);
        if(timeView!=null&&timeView.getText().length()!=0)
        	c=TimeUtils.getCalendarFromFormat(timeView.getText().toString(), TimeUtils.TIME_FORMAT);
        
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

        // Create a new instance of TimePickerDialog and return it
        TimePickerDialog pickerDialog= new TimePickerDialog(getActivity(), this, hour, minute,
                DateFormat.is24HourFormat(getActivity()));
        
        
        return pickerDialog;
        
    }

	@Override
	public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
	      timeView=(EditText) getActivity().findViewById(timeViewId);
		timeView.setText(((hourOfDay<10)? ("0"+hourOfDay):hourOfDay)+ ":"+((minute<10)? ("0"+minute):minute ));
	}

	

    
}